package cn.itsource.blog.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.itsource.blog.domain.User;
import cn.itsource.blog.mapper.UserMapper;
import cn.itsource.blog.service.IUserService;
import cn.itsource.query.BaseQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public PageInfo<User> pageInfo(BaseQuery baseQuery) {
        PageHelper.startPage(baseQuery.getCurrentPage(), baseQuery.getPageSize());
        List<User> all = userMapper.getAllByQuery(baseQuery);
        return new PageInfo<>(all);
    }

    @Override
    public void save(User user) {
        if (user.getId() == null) {
            user.setPassword(SecureUtil.md5(user.getPassword()));
            userMapper.save(user);
        }else{
            userMapper.update(user);
        }
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public void batchDelete(List<User> users) {
        userMapper.batchDelete(users);
    }


}
